package de.backtomedieval.activities.gamemenu;

import de.backtomedieval.R;
import de.backtomedieval.activities.TopBar;
import de.backtomedieval.main.Game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

public class ViewUnits extends Activity {
	
	private Handler mHandler = new Handler();
	private int refresh = Game.getRefreshrate();
	private TopBar topbar;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
       super.onCreate(savedInstanceState);
       setContentView(R.layout.viewunitsincity); 
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
       
		topbar = new TopBar(this);
		topbar.setTitle(Game.getGame().getcCity().getName() + ": "+ getString(R.string.units));
		
       refreshUnitsInCity();
    }
    

	public void refreshUnitsInCity() {
		TextView unitsincity = (TextView) findViewById(R.id.unitsincity);
		unitsincity.setText(Game.getGame().getcCity().getUnits().toString());
	}


	private Runnable mUpdateUnitsInCityTask = new Runnable() {
		   public void run() {
			   Log.v("Update", "UnitsInCity: Cityinfo");
			   refreshUnitsInCity();
			   topbar.refreshCityInfo();
		       mHandler.postDelayed(this,refresh);
		   }
	};
	
	@Override
	public void onResume(){
	   super.onResume();
	   Game.setLastActivity(this);
       mHandler.removeCallbacks(mUpdateUnitsInCityTask);
       mHandler.postDelayed(mUpdateUnitsInCityTask, 100);
	}
	
	
	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onPause() {
		super.onPause();
		mHandler.removeCallbacks(mUpdateUnitsInCityTask);
	}
	
	@Override
	public void onBackPressed() {
		mHandler.removeCallbacks(mUpdateUnitsInCityTask);
		Intent changeToIntent = new Intent(this,Gamemenu.class);
		startActivity(changeToIntent);
		this.finish();
		return;
	}
	
	
}
